//处理地图对象

/**
 * 截取坐标
 * @param {Object} overlays 百度地图形状对象
 */
export function cutPoints(overlays){
    if(!overlays) return {};
    let result = {},
        ias = [],
        point = '';
    for(let i in overlays){
        if(!overlays[i].id) continue;
        result[i] = {};
        result[i].id = overlays[i].id;
        result[i].name = overlays[i].name;
        result[i].type = overlays[i].type;
        result[i].style = overlays[i].style;
        point = '';
        
        if(overlays[i].type=='point'){
            point = overlays[i].overlay.point.lng + ',' + overlays[i].overlay.point.lat;
        }else{
            ias = overlays[i].overlay.ia;
            for(let j=0, len=ias.length; j<len; j++){
                point += ';' + ias[j].lng + ',' + ias[j].lat;
            }
            point = point.substr(1);
        }
        result[i].point = point;
    }
    return result;
}
/**
 * 从形状中仅提取坐标
 * @param {String} type 形状类型
 * @param {Object} overlays 形状
 */
export function transPoints(type, overlay){
    if(!overlay || !type) return null;
    let ias = [],
        point = '';
    if(type=='point'){
        point = overlay.point.lng + ',' + overlay.point.lat;
    }else{
        ias = overlay.ia;
        for(let j=0, len=ias.length; j<len; j++){
            point += ';' + ias[j].lng + ',' + ias[j].lat;
        }
        point = point.substr(1);
    }
    return point;
}
/**
 * 添加点
 */
export function addPoint(mapObj, item){
    let marker = null,
        point = item.point.split(',');
    marker = new BMap.Marker(new BMap.Point(point[0], point[1]));
    return marker;
}
/**
 * 添加线
 */
export function addLine(mapObj, item){
    let line = null,
        point = item.point.split(';'),
        pointArr = [],
        style = {
            strokeColor:"blue", 
            strokeWeight:2, 
            strokeOpacity:0.5
        },
        x, y;
    for(let i=0, len=point.length; i<len; i++){
        x = point[i].split(',')[0];
        y = point[i].split(',')[1];
        pointArr.push(new BMap.Point(x, y));
    }
    line = new BMap.Polyline(pointArr, Object.assign(style, item.style));
    return line;
}
/**
 * 添加面
 */
export function addPolygon(mapObj, item){
    let polygon = null,
        point = item.point.split(';'),
        pointArr = [],
        style = {
            strokeColor:"blue", 
            strokeWeight:2, 
            strokeOpacity:0.5
        },
        x, y;
    for(let i=0, len=point.length; i<len; i++){
        x = point[i].split(',')[0];
        y = point[i].split(',')[1];
        pointArr.push(new BMap.Point(x, y));
    }
    polygon = new BMap.Polygon(pointArr, Object.assign(style, item.style));
    return polygon;
}